<?php
/*
 * @Author: your name
 * @Date: 2020-11-23 12:54:46
 * @LastEditTime: 2020-12-31 17:27:38
 * @LastEditors: Please set LastEditors
 * @Description: 规则基础类
 * @FilePath: /special/application/adminapiv3/service/module_manage/netfilter/rule_log_service/RuleLogRuleService.php
 */
namespace app\adminapiv3\service\module_manage\netfilter\rule_log_service;

use app\adminapiv3\service\BaseService;

class RuleLogRuleService extends BaseService
{
    //处理方式 同意
    const PROCESS_ALLOW = 1;
    //处理方式 拒绝
    const PROCESS_DROP = 2;
    //流量方向 出站
    const DIRECTION_OUTPUT = 1;
    //流量方向 入站
    const DIRECTION_INPUT = 2;

    //规则内容
    protected $content = '';
    //源ip
    protected $src_ip = [];
    //目标ip
    protected $dest_ip = [];
    //源端口
    protected $src_port = [];
    //目标端口
    protected $dest_port = [];
    //流量方向
    protected $direction = 1;
    //处理方式
    protected $process = 2;
    //协议
    protected $protocols = 3;
    //规则应用域
    public $scope = '';
    //规则状态
    private $status = 1;
    //规则类型
    private $type = 1;
    //主机id
    private $host_id;

    function __construct($host_id, $content)
    {
        $this->host_id = $host_id;
        $this->content = strtolower($content);
    }

    /**
     * @name: 获取数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 14:33:30
     */
    public function getData ($fid, $alias)
    {
        $return = [];
        $id = $fid;

        foreach ($this->dest_port as $destPort)
        {
            foreach ($this->src_port as $srcPort)
            {
                $return[] = [
                    'id' => $id++,
                    'fid' => $fid,
                    'host_id' => $this->host_id,
                    'direction' => $this->direction,
                    'protocols' => $this->protocols,
                    'process' => $this->process,
                    'status' => $this->status,
                    'type' => $this->type,
                    'src_ip_s' => $this->src_ip[0],
                    'src_ip_e' => $this->src_ip[1],
                    'dest_ip_s' => $this->dest_ip[0],
                    'dest_ip_e' => $this->dest_ip[1],
                    'src_port_s' => $srcPort[0],
                    'src_port_e' => $srcPort[1],
                    'dest_port_s' => $destPort[0],
                    'dest_port_e' => $destPort[1],
                    'alias' => '',
                ];
            }
        }

        $return[0]['scope'] = $this->scope;
        $return[0]['alias'] = $alias;

        return $return;
    }
    
    /**
     * @name: 处理端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 14:00:24
     */
    protected function processPort ($math, $delimiter = ':')
    {
        if (empty($math))
        {
            return [
                [
                    0,
                    65535
                ]
            ];
        }
        
        $return = [];
        $math_a = explode(',', $math[1]);
        foreach ($math_a as $math_s)
        {
            $math_sa = explode($delimiter, $math_s);
            if(count($math_sa) < 2)
            {
                $return[] = [
                    $math_sa[0],
                    $math_sa[0]
                ];
            }
            else
            {
                $return[] = [
                    $math_sa[0],
                    $math_sa[1]
                ];
            }
        }
        return $return;
    }

    /**
     * @name: 处理ip
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 14:02:55
     */
    protected function processIp ($math)
    {
        if (empty($math))
        {
            return [
                0,
                4294967295
            ];
        }

        $return = [];

        if(stristr($math[1], '-'))
        {
            $return = [
                sprintf('%u', ip2long($math[1])),
                sprintf('%u', ip2long($math[1]))
            ];
        }
        else
        {
            $math_a = explode('-', $math[1]);
            $return = [
                sprintf('%u', ip2long($math_a[0])),
                sprintf('%u', ip2long($math_a[1]))
            ];
        }
        
        return $return;
    }
}